# Author: HZT
# Date: 2023/8/28
import unittest
from config.config_loader import config
from utils import UtilsDriver, login_with_session, get_cid_by_username
from page.web_sk_pm import WebSkPm
from page.web_sk_top import WebSkTop
from utils import DBUtil
import time


class TestWebSkPm(unittest.TestCase):
    credentials = None
    skPm = None
    driver = None

    @classmethod
    def setUpClass(cls) -> None:
        cls.driver = UtilsDriver.get_web_driver()
        # 实例化
        cls.skPm = WebSkPm(cls.driver)
        cls.skTop = WebSkTop(cls.driver)
        # 打开网页
        cls.driver.get(config.URL)
        login_with_session('system2', cls.driver)
        # 点击人员管理
        cls.skPm.sk_pm_click_rygl()

    @classmethod
    def tearDownClass(cls):
        # 在所有测试用例执行完毕后，关闭浏览器
        # UtilsDriver.get_web_driver().quit()
        pass

    # 页面数据条数与数据库数量一致
    def test01_sk_pm(self):
        # 获取公司ID
        cid = get_cid_by_username(config.CREDENTIALS["username_sk"])
        # 通过id获取公司有效用户数
        count = DBUtil.select_one(
            "SELECT count(*) from admin_users where id in (SELECT admin_user_id FROM company_users where company_id={}) and `deleted_at` IS NULL;".format(
                cid))
        count = str(count[0])
        # 页面获取实际总条数
        total = self.skPm.sk_pm_get_total()
        # 断言
        self.assertEqual(total, count)

    # 新增成功(所有数据有效)
    def test02_sk_pm(self):
        # 获取当前时间戳
        timestamp = str(int(time.time()))
        username = 'test' + timestamp
        # 新增
        self.skPm.sk_pm_xz(username, username, "qwe123")
        # 获取弹窗
        text = self.skPm.sk_pm_get_cg()
        # 断言
        self.assertIn("成功", text)

    # 新增失败（用户名不输入）
    def test03_sk_pm(self):
        # 获取当前时间戳
        timestamp = str(int(time.time()))
        username = 'test' + timestamp
        # 新增
        self.skPm.sk_pm_xz("", username, "qwe123")
        # 获取弹窗
        text = self.skPm.sk_pm_get_usernamets()
        # 断言
        self.assertIn("The 用户名 field is required", text)

    # 新增失败（名称不输入）
    def test04_sk_pm(self):
        self.skPm.sk_pm_click_rygl()
        # 获取当前时间戳
        timestamp = str(int(time.time()))
        username = 'test' + timestamp
        # 新增
        self.skPm.sk_pm_xz(username, "", "qwe123")
        # 获取弹窗
        text = self.skPm.sk_pm_get_namets()
        # 断言
        self.assertIn("The 名称 field is required", text)

    # 新增失败（密码不输入）
    def test05_sk_pm(self):
        self.skPm.sk_pm_click_rygl()
        # 获取当前时间戳
        timestamp = str(int(time.time()))
        username = 'test' + timestamp
        # 新增
        self.skPm.sk_pm_xz(username, username, "")
        # 获取弹窗
        text = self.skPm.sk_pm_get_pwdts()
        # 断言
        self.assertIn("The 密码 field is required", text)

    # 快速创建成功(所有数据有效)
    def test06_sk_pm(self):
        self.skPm.sk_pm_click_rygl()
        # 获取当前时间戳
        timestamp = str(int(time.time()))
        username = 'test' + timestamp
        # 快速创建
        self.skPm.sk_pm_kscj(username, username, "qwe123")
        # 获取弹窗
        text = self.skPm.sk_pm_get_cg()
        # 断言
        self.assertIn("成功", text)

    # 编辑成功(所有数据有效)
    def test07_sk_pm(self):
        # 获取当前时间戳
        timestamp = str(int(time.time()))
        username = 'test' + timestamp
        # 编辑
        self.skPm.sk_pm_bj(username, username, "qwe123")
        # 获取弹窗
        text = self.skPm.sk_pm_get_cg()
        # 断言
        self.assertIn("成功", text)

    # 删除成功
    def test08_sk_pm(self):
        self.skPm.sk_pm_click_rygl()
        # 点击删除
        self.skPm.sk_pm_click_cz()
        self.skPm.sk_pm_click_sc()
        time.sleep(1)
        self.skPm.sk_pm_click_sctj()
        time.sleep(1)
        text = self.skPm.sk_pm_get_sccg()
        # 断言
        self.assertIn("成功", text)
        self.skPm.sk_pm_click_scok()
